﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.Threading;

namespace GOM_PLAYER
{
    public partial class Preferences : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        process_xml process_xml = new process_xml();
        user_defined_keyboard udk;
        public main_window main_window;
        public Preferences(main_window mw)
        {
            this.main_window = mw;
            InitializeComponent();
        }
        public Preferences()
        {
            InitializeComponent();
        }

       
        private void Preferences_Load(object sender, EventArgs e)
        {
           
        }

        private void Settings_group_Paint(object sender, PaintEventArgs e)
        {

        }

        private void use_prefered_color_Click(object sender, EventArgs e)
        {
            this.main_window.showMessage("Open color dialog");
            colorDialog.ShowDialog();
        }

        private void Auto_backgroud_color_ValueChanged(object sender, EventArgs e)
        {
            this.main_window.showMessage("Change value color");
            if (Auto_backgroud_color.Checked == false)
            {
                Use_prefered_color.Enabled = false;
            }
            else
            {
                Use_prefered_color.Enabled = true;
            }

        }

        private void Use_prefered_color_Click_1(object sender, EventArgs e)
        {
            this.main_window.showMessage("Open color dialog");
            colorDialog.ShowDialog();
        }

        private void Add_logo_button_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "All Files (*.*)|*.*|JPG|*.jpg|PNG|*.png";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string file = ofd.FileName;
                    logo logo = new logo(file, file.Remove(0, file.LastIndexOf("\\", file.Length - 1)).Remove(0, 1));

                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewCell dcell;
                    dcell = new DataGridViewTextBoxCell();
                    dcell.Value = logo.name_logo;
                    dr.Cells.Add(dcell);

                    dcell = new DataGridViewButtonCell();
                    dcell.Value = "Delete";
                    dr.Cells.Add(dcell);

                    dcell = new DataGridViewButtonCell();
                    dcell.Value = "open directory";
                    dr.Cells.Add(dcell);

                    process_xml.insert_logo(logo);
                    logo_dataGridView.Rows.Add(dr);
                    logo_dataGridView.Rows[logo_dataGridView.Rows.Count-1].Selected = true;
                    display_radioGroup.Enabled = true;
                    
                }
            }
        }

        public uint Color2Uint32(Color clr)
        {

            int t;
            byte[] a;

            t = ColorTranslator.ToOle(clr);

            a = BitConverter.GetBytes(t);

            return BitConverter.ToUInt32(a, 0);

        }
       
      

        private void Get_more_logos_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://player.gomlab.com/eng/logo/");
        }

      

        private void Preferences_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
        }

        private void Add_shortcut_keyboard_Click(object sender, EventArgs e)
        {
            udk = new user_defined_keyboard(this);
            udk.Show();
        }

      

      

        private void Add_skin_button_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "XML (*.xml)|*.xml";
                if (ofd.ShowDialog() == DialogResult.OK)
                {  
                    skin sk= process_xml.load_skin(ofd.FileName);
                    DataGridViewRow row = new DataGridViewRow();
                    DataGridViewCell cell;

                    cell = new DataGridViewTextBoxCell();
                    cell.Value = sk.name_skin;
                    row.Cells.Add(cell);


                    cell = new DataGridViewTextBoxCell();
                    cell.Value = sk.file_name;
                    row.Cells.Add(cell);

                    cell = new DataGridViewButtonCell();
                    cell.Value = "Delete";
                    row.Cells.Add(cell);

                    cell = new DataGridViewButtonCell();
                    cell.Value = "Open directory";
                    row.Cells.Add(cell);

                    skin_dataGridView.Rows.Add(row);

                    //main_window.list_skin.Add(sk);
                    process_xml.insert_skin(ofd.FileName);
                }
            }
        }

        public void skin_dataGridView_SelectionChanged(object sender, EventArgs e)
        {
            select_skin();
        }

        private void theme_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            main_window.BackgroundImage = Image.FromFile(theme_comboBox.SelectedValue.ToString());
            main_window.layoutControlGroup4.ContentImage = Image.FromFile(theme_comboBox.SelectedValue.ToString());     
        }

        public void use_skin(skin sk)
        {
             main_window.play_button.Image = Image.FromFile( sk.play );
             main_window.path_image_play_button=sk.play;
             main_window.path_image_pause_button = sk.pause;
             main_window.stop_button.Image = Image.FromFile(sk.stop);
             main_window.next_file_button.Image = Image.FromFile(sk.nextfile);
             main_window.previous_file_button.Image = Image.FromFile(sk.previousfile);
             main_window.volume_button.Image = Image.FromFile(sk.volume);
             main_window.path_image_volume_button = sk.volume;
             main_window.path_image_volume_mute_button = sk.volume_mute;
             main_window.full_screen_button.Image = Image.FromFile(sk.fullscreen);
             main_window.control_panel_button.Image = Image.FromFile(sk.controlpanel);
             main_window.play_list_button.Image = Image.FromFile(sk.playlist);
            
        }

        public void select_skin()
        {
            theme_comboBox.DisplayMember = "name";
            theme_comboBox.ValueMember = "path";
            int index = new int();
            if (skin_dataGridView.SelectedRows.Count > 0)
            {
                index = skin_dataGridView.SelectedRows[0].Index;
                List<string> list_path_skin=process_xml.load_skin_inserted();
                skin sk = new skin();
                sk = process_xml.load_skin(list_path_skin[index]);
                theme_comboBox.DataSource = sk.theme;
                use_skin(sk);
                skin_info_textBox.Text = sk.info;
                main_window.change_status_pause();
            }
        }

        private void skin_dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            switch (e.ColumnIndex)
            {
                case 2:
                    if (e.RowIndex != 0)
                    {
                        skin_dataGridView.Rows.RemoveAt(e.RowIndex);
                        process_xml.delete_skin(e.RowIndex);
                    }
                    else
                    {
                        MessageBox.Show("Unable delete Skin default","Message");
                    }
                    break;
                case 3:
                    OpenFileDialog ofd;
                    using (ofd = new OpenFileDialog())
                    {
                        ofd.ShowReadOnly = true;
                        List<string> list_path_skin=process_xml.load_skin_inserted();
                        skin sk = new skin();
                        sk = process_xml.load_skin(list_path_skin[e.RowIndex]);
                        ofd.FileName = sk.theme[0].path;
                        ofd.ShowDialog();
                    }
                    break;
            }
        }

      

        private void Get_more_skin_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://player.gomlab.com/eng/skin/");
        }

        private void logo_dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            switch(e.ColumnIndex)
            {
                case 1:
                    if (e.RowIndex != 0)
                    {
                        logo_dataGridView.Rows.RemoveAt(e.RowIndex);
                        process_xml.delete_logo(e.RowIndex);
                    }
                    else
                        MessageBox.Show("Unable delete logo default", "Message");
                    break;
                case 2:
                    if (logo_dataGridView.Rows.Count < 1)
                        display_radioGroup.Enabled = false;
                    List<logo> list_logo=process_xml.load_logo_inserted();
                    string path = list_logo[e.RowIndex].path;
                    OpenFileDialog ofd;
                    using (ofd = new OpenFileDialog())
                    {
                        ofd.ShowReadOnly = true;
                        ofd.FileName = path;
                        ofd.ShowDialog();
                    }
                    break;
            }
        }

        private void logo_dataGridView_Click(object sender, EventArgs e)
        {

            if (main_window.stop_button.Enabled == false)
            {
                Thread th = new Thread(show_logo2);
                th.Start();
            }
        }

        public void show_logo()
        {
            if (logo_dataGridView.SelectedRows.Count > 0)
            {
                main_window.axMoviePlayer.FileName = "screen\\black.flv";
                main_window.axMoviePlayer.Play();
                List<logo> list_logo = process_xml.load_logo_inserted();
                main_window.axMoviePlayer.DrawImage((float)0.1, 0, 0, list_logo[logo_dataGridView.SelectedRows[0].Index].path, Color2Uint32(Color.FromArgb(0, 0, 0)), 1, 125);
                main_window.axMoviePlayer.Refresh();
                main_window.temp_index_logo = logo_dataGridView.SelectedRows[0].Index;
                Thread.Sleep(20000);
            }
        }

        public void show_logo2(object index)
        {
            if (logo_dataGridView.SelectedRows.Count > 0)
            {
                main_window.axMoviePlayer.FileName = "screen\\black.flv";
                main_window.axMoviePlayer.Play();
                main_window.axMoviePlayer.Refresh();
                List<logo> list_logo = process_xml.load_logo_inserted();
                main_window.axMoviePlayer.DrawImage((float)0.1, 0, 0, list_logo[int.Parse(index.ToString())].path, Color2Uint32(Color.FromArgb(0, 0, 0)), 1, 125);
                Thread.Sleep(20000);
            }
        }

        private void logo_dataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (main_window.stop_button.Enabled == false)
            {
                Thread th = new Thread(show_logo);
                th.Start();
            }
        }

        private void left_single_Pause_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            left_single_Play_barCheckItem.Checked = false;
            left_single_Stop_barCheckItem.Checked = false;
            left_single_Pause_barCheckItem.Checked = true;
            left_single_Nextfile_barCheckItem.Checked = false;
            left_single_Previousfile_barCheckItem.Checked = false;
            left_single_default_barCheckItem.Checked = false;
            shortcut_mouse sm = new shortcut_mouse();
            sm.shortcut_key = "1";
            sm.description = "Pause";
            process_xml.insert_shortcut_mouse(sm, 0);
            main_window.list_shortcut_mouse = process_xml.load_shortcut_mouse();
        }

        private void left_single_Stop_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            left_single_Play_barCheckItem.Checked = false;
            left_single_Stop_barCheckItem.Checked = true;
            left_single_Pause_barCheckItem.Checked = false;
            left_single_Nextfile_barCheckItem.Checked = false;
            left_single_Previousfile_barCheckItem.Checked = false;
            left_single_default_barCheckItem.Checked = false;
            shortcut_mouse sm = new shortcut_mouse();
            sm.shortcut_key = "1";
            sm.description = "Stop";
            process_xml.insert_shortcut_mouse(sm, 0);
            main_window.list_shortcut_mouse = process_xml.load_shortcut_mouse();
        }

        private void left_single_Nextfile_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            left_single_Play_barCheckItem.Checked = false;
            left_single_Stop_barCheckItem.Checked = false;
            left_single_Pause_barCheckItem.Checked = false;
            left_single_Nextfile_barCheckItem.Checked = true;
            left_single_Previousfile_barCheckItem.Checked = false;
            left_single_default_barCheckItem.Checked = false;
            shortcut_mouse sm = new shortcut_mouse();
            sm.shortcut_key = "1";
            sm.description = "Next file";
            process_xml.insert_shortcut_mouse(sm, 0);
            main_window.list_shortcut_mouse = process_xml.load_shortcut_mouse();
        }

        private void left_single_Previousfile_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            left_single_Play_barCheckItem.Checked = false;
            left_single_Stop_barCheckItem.Checked = false;
            left_single_Pause_barCheckItem.Checked = false;
            left_single_Nextfile_barCheckItem.Checked = false;
            left_single_Previousfile_barCheckItem.Checked = true;
            left_single_default_barCheckItem.Checked = false;
            shortcut_mouse sm = new shortcut_mouse();
            sm.shortcut_key = "1";
            sm.description = "Previous file";
            process_xml.insert_shortcut_mouse(sm, 0);
            main_window.list_shortcut_mouse = process_xml.load_shortcut_mouse();
        }

        private void left_single_default_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            left_single_Play_barCheckItem.Checked = false;
            left_single_Stop_barCheckItem.Checked = false;
            left_single_Pause_barCheckItem.Checked = false;
            left_single_Nextfile_barCheckItem.Checked = false;
            left_single_Previousfile_barCheckItem.Checked = false;
            left_single_default_barCheckItem.Checked = true;
            shortcut_mouse sm = new shortcut_mouse();
            sm.shortcut_key = "1";
            sm.description = "Default";
            process_xml.insert_shortcut_mouse(sm, 0);
            main_window.list_shortcut_mouse = process_xml.load_shortcut_mouse();
        }

        private void Play_barCheckItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            left_single_Play_barCheckItem.Checked = true;
            left_single_Stop_barCheckItem.Checked = false;
            left_single_Pause_barCheckItem.Checked = false;
            left_single_Nextfile_barCheckItem.Checked = false;
            left_single_Previousfile_barCheckItem.Checked = false;
            left_single_default_barCheckItem.Checked = false;
            shortcut_mouse sm = new shortcut_mouse();
            sm.shortcut_key = "1";
            sm.description = "Play";
            process_xml.insert_shortcut_mouse(sm, 0);
            main_window.list_shortcut_mouse = process_xml.load_shortcut_mouse();
        }

        private void Auto_backgroud_color_Click(object sender, EventArgs e)
        {
            if (Auto_backgroud_color.Checked == true)
                Use_prefered_color.Enabled = false;
            else
                Use_prefered_color.Enabled = true;
        }

        private void Use_prefered_color_Click_2(object sender, EventArgs e)
        {
            colorDialog.ShowDialog();
        }

        private void check_version_auto_checkcheckEdit_CheckedChanged(object sender, EventArgs e)
        {
            if (check_version_auto_checkcheckEdit.Checked == true)
            {
                notify_major_checkEdit.Enabled = true;
                notify_minor_checkEdit.Enabled = true;
            }
            else
            {
                notify_major_checkEdit.Enabled = false;
                notify_minor_checkEdit.Enabled = false;
            }
        }

        private void Reset_simpleButton_Click(object sender, EventArgs e)
        {
            DialogResult myDialogResult;
            myDialogResult = MessageBox.Show("Do you want reset Gom Player ?", "Message", MessageBoxButtons.YesNo);

            if (myDialogResult == DialogResult.Yes)
            {
                main_window.pr.Dispose();
                main_window.ctp.Dispose();
                main_window.temp_control_panel_button += 2;
                main_window.temp_preferences_barbutton++;
                main_window.temp_reset_preferences = true;
                main_window.init();
            }

            if (myDialogResult == DialogResult.No)
            {

            }
 
          
        }

        private void Close_simpleButton_Click_1(object sender, EventArgs e)
        {
            this.Hide();
            main_window.temp_preferences_barbutton++;
        }

        private void Delete_shortcut_keyboard_Click(object sender, EventArgs e)
        {
            process_xml.delete_shortcut_keyboard(user_keyboard_defined_dataGridVew.SelectedRows[0].Index);
            user_keyboard_defined_dataGridVew.DataSource = process_xml.load_shortcut_keyboard();
        }
    
    }
}